package com.bj58.migrate.update.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.bj58.migrate.ScfProxy;
import com.bj58.migrate.bll.CMCSBll;
import com.bj58.migrate.condition.Constant;
import com.bj58.migrate.helper.StringHelper;
import com.bj58.migrate.model.AvgJZSalaryModel;
import com.bj58.migrate.update.DataHandler;
import com.bj58.sfft.cmc.entity.Category;
import com.bj58.sfft.cmc.entity.Local;

/**
 * 兼职平均薪资计算
 */
public class AvgJZCrawlHandleImpl implements DataHandler{

	private Logger logger = Logger.getLogger( AvgJZCrawlHandleImpl.class );
	private static final String[] salaryFilter = { "&source=0|6", "&param5543=0_50&source=0|6", "&param5543=51_150&source=0|6", "&param5543=151_250&source=0|6", "&param5543=251_350&source=0|6", "&param5543=351_500&source=0|6", "&param5543=501_1000&source=0|6", "&param5543=1000_999999&source=0|6" };

	public void handle( String condition, String[] args ){

		logger.info( "Handling.... " + args[ 2 ] + " " + condition );

		AvgJZSalaryModel business = new AvgJZSalaryModel();

		try{
			condition = JZConditonParser.parseAndSet( condition, business );
		}
		catch( Exception ex ){
			logger.error( "JZConditonParser.parseAndSet:" + condition, ex );
		}
		try{
			int infocount = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 0 ] );
			int countless_50 = 0, count50_150 = 0, count150_250 = 0, count250_350 = 0, count350_500 = 0, count500_1000 = 0, countthan_1000 = 0, count_mianyi = 0;

			if( infocount > 0 ){
				countless_50 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 1 ] );
				count50_150 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 2 ] );
				count150_250 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 3 ] );
				count250_350 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 4 ] );
				count350_500 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 5 ] );
				count500_1000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 6 ] );
				countthan_1000 = ScfProxy.ses_SESAgent().getCount( condition + salaryFilter[ 7 ] );
				count_mianyi = infocount - countless_50 - count50_150 - count150_250 - count250_350 - count350_500 - count500_1000 - countthan_1000;
			}
			business.setInfocount( infocount );
			business.setJiesuanfangshi( StringHelper.tryParseInt( args[ 3 ], 1 ) );// 薪资结算方式,默认日结1
			business.setCount_mianyi( count_mianyi > 0 ? count_mianyi : 0 );
			business.setCountless_50( countless_50 );
			business.setCount50_150( count50_150 );
			business.setCount150_250( count150_250 );
			business.setCount250_350( count250_350 );
			business.setCount350_500( count350_500 );
			business.setCount500_1000( count500_1000 );
			business.setCountthan_1000( countthan_1000 );
			business.caculateAvg();
			business.write();
			business.show();
		}
		catch( Exception e ){
			logger.error( "AvgZJCrawlHandleImpl get infos Exception:" + condition, e );
			e.printStackTrace();
		}
	}
}

class JZConditonParser{

	private static final Pattern cate_pattern = Pattern.compile( "cateid=(\\d+)" );
	private static final Pattern local_pattern = Pattern.compile( "local=(\\d+)" );
	private static final Pattern post_pattern = Pattern.compile( "postdate=(\\d+)_(\\d+)" );
	private static final Pattern pay_way_pattern = Pattern.compile( "param=(\\d+)_(\\d+)" );

	/**
	 * 分析condition，获取类别，地域信息<br>
	 * 并设置对象相关属性
	 * 
	 * @param condtion
	 * @param model
	 */
	public static String parseAndSet( String condtion, AvgJZSalaryModel model ){

		Matcher mccate = cate_pattern.matcher( condtion );
		Matcher mclocal = local_pattern.matcher( condtion );
		Matcher mpost = post_pattern.matcher( condtion );
		Matcher mpay = pay_way_pattern.matcher( condtion );

		int level2 = -1;
		int local = -1;
		int start = 0;
		int end = 0;

		if( mccate.find() ){

			level2 = Integer.parseInt( mccate.group( 1 ) );

			// 对学生兼职特殊处理
			if( Constant.XueShengJianZhiCateId == level2 ){
				condtion = condtion.replace( "cateid=" + Constant.XueShengJianZhiCateId, "cateid=686|1122|1123|687|690" );// cateid=13945|28923|28924|13946|13948
				System.out.println( "condtion:" + condtion );
				model.setCateid( level2 );
				model.setCatename( "学生兼职" );
				model.setExt( "xueshengjianzhi" );
			}
			else{
				Category ext = CMCSBll.GetCatetory( level2 );
				model.setCateid( level2 );
				if( ext != null ){
					model.setCatename( ext.getCateName() );
					model.setExt( ext.getDirName() );// 兼职次值存在严重和线上不匹配bug（cmc中的--线上url使用的），比如化妆师(jzhuazhuang--jzmakeup)、司仪(siyiyanchu--jzwenyi)、视频(shipinzhizuo--jianjizhizuo)、法务（jzfawu--daguansi）、网络维修（wangluobuxian--jzbuxian）、服务员（jzfuwuyuan--jianzhifwy）艺术老师（yishulaoshi--jzyishu）网站建设（jzjisuanji--jisuanjiwl）摄影摄像（jzsheying--jzphoto）
				}
			}
		}

		if( mclocal.find() ){

			local = Integer.parseInt( mclocal.group( 1 ) );
			Local localext = CMCSBll.GetDispLocal( local );

			if( localext != null ){
				model.setLocalname( localext.getLocalName() );
			}
			model.setLocalid( local );
		}

		if( mpost.find() ){

			start = Integer.parseInt( mpost.group( 1 ) );
			end = Integer.parseInt( mpost.group( 2 ) );

			model.setStartdate( start );
			model.setEnddate( end );
		}
		return condtion;
	}

}